package kata15;

import kata06.Arrange;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Diversion {
    /**
     * 获取不相邻的总数
     * @param length 二级制数的长度
     * @param notAdjacent 排除相邻的数据
     */
    public int getBinaryAdjacentBits(int length,String notAdjacent){
        if(length < 1){
            return 0;
        }
        Set<String> allBinary = getAllBinary(length);
        System.out.println("allBinary : " + allBinary.toString());

        int notAdjacentNumber = 0;
        for (String binary : allBinary) {
            //字符串中不包含 noAdjacent 则证明不相邻
            if(binary.indexOf(notAdjacent) < 0){
                ++notAdjacentNumber;
            }
        }
        return notAdjacentNumber;
    }

    public Set<String> getAllBinary(int length){
        //生成全排序的字符
        List<String> numberStringList = generateNumberStringList(length);
//        System.out.println("genericNumberStrList : " + numberStrList.toString());

        int numberStringListSize = numberStringList.size();
        //使用 set容器  去重
        Set<String> allSort = new HashSet<>();
        allSort.add(numberStringList.get(0));
        allSort.add(numberStringList.get(numberStringListSize - 1));

        //排除第一个 和 最后一个 （首和位不加入全排序）
        for(int i = 1; i < (numberStringListSize -1); i++){
            allSort.addAll(Arrange.getAllSort(numberStringList.get(i)));
        }
        //打印全排列后的数组
//        System.out.println("allSort : " + allSort.toString());
        return allSort;
    }

    /**
     * 生成二进制字符串
     * 当 length = 2 -> 00 10 11
     * 当 length = 3 -> 000 100 110 111
     * 当 length = 4 -> 0000 1000 1100 1110 1111
     * @param length
     * @return
     */
    private List<String> generateNumberStringList(int length){
        if(length < 1){
            return new ArrayList<>();
        }
        List<String> numberStringList = new ArrayList<>();
        StringBuilder numberStingBuilder= new StringBuilder();
        for(int i = 0; i < length; i++){
            numberStingBuilder.append("0");
        }

        //第一次加入
        numberStringList.add(numberStingBuilder.toString());
        for(int i = 0; i < length; i++){
            numberStringList.add(numberStingBuilder.replace(i,i+1,"1").toString());
        }
        return numberStringList;
    }
}
